package view;
import javax.swing.*;

import controller.Convert;

import net.miginfocom.swing.MigLayout;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.*;

public class MainMenu extends JFrame	{
	private static final long serialVersionUID = 1L;
	public static JTextField usaF=new JTextField(9);
	public static JTextField jpnF=new JTextField(9);
	public static JTextField bulF=new JTextField(9);
	public static JTextField czkF=new JTextField(9);
	public static JTextField dkkF=new JTextField(9);
	public static JTextField gbpF=new JTextField(9);
	public static JTextField hufF=new JTextField(9);
	public static JTextField ltlF=new JTextField(9);
	public static JTextField lvlF=new JTextField(9);
	public static JTextField plnF=new JTextField(9);
	public static JTextField ronF=new JTextField(9);
	public static JTextField sekF=new JTextField(9);
	public static JTextField chfF=new JTextField(9);
	public static JTextField nokF=new JTextField(9);
	public static JTextField hrkF=new JTextField(9);
	public static JTextField rubF=new JTextField(9);
	public static JTextField tryF=new JTextField(9);
	public static JTextField audF=new JTextField(9);
	public static JTextField brlF=new JTextField(9);
	public static JTextField cadF=new JTextField(9);
	public static JTextField cnyF=new JTextField(9);
	public static JTextField hkdF=new JTextField(9);
	public static JTextField idrF=new JTextField(9);
	public static JTextField ilsF=new JTextField(9);
	public static JTextField inrF=new JTextField(9);
	public static JTextField krwF=new JTextField(9);
	public static JTextField mxnF=new JTextField(9);
	public static JTextField myrF=new JTextField(9);
	public static JTextField nzdF=new JTextField(9);
	public static JTextField phpF=new JTextField(9);
	public static JTextField sgdF=new JTextField(9);
	public static JTextField thbF=new JTextField(9);
	public static JTextField zarF=new JTextField(9);
	public static JTextField dateF=new JTextField(8);
	public MainMenu(String uname) throws IOException	{
		JButton conv=new JButton("Convert");
		JButton exit=new JButton("Log Out");
		ImageIcon eurI=new ImageIcon("resources/png/European-Union-Flag-64.png");
		JLabel eurL=new JLabel(eurI);
		final JTextField eurF=new JTextField(9);
		ImageIcon usaI=new ImageIcon("resources/png/United-States-Flag-24.png");
		JLabel usaL=new JLabel(usaI);
		usaL.setToolTipText("United States dollar");
		
		ImageIcon jpnI=new ImageIcon("resources/png/Japan-Flag-24.png");
		JLabel jpnL=new JLabel(jpnI);
		jpnL.setToolTipText("Japanese yen");
		
		ImageIcon bulI=new ImageIcon("resources/png/Bulgaria-Flag-24.png");
		JLabel bulL=new JLabel(bulI);
		bulL.setToolTipText("Bulgarian lev");
		
		ImageIcon czkI=new ImageIcon("resources/png/Czech-Republic-Flag-24.png");
		JLabel czkL=new JLabel(czkI);
		czkL.setToolTipText("Czech Republic koruna");
		
		ImageIcon dkkI=new ImageIcon("resources/png/Denmark24.png");
		JLabel dkkL=new JLabel(dkkI);
		dkkL.setToolTipText("Danish krone");
		
		ImageIcon gbpI=new ImageIcon("resources/png/United-Kingdom-flag-24.png");
		JLabel gbpL=new JLabel(gbpI);
		gbpL.setToolTipText("British pounds");
		
		ImageIcon hufI=new ImageIcon("resources/png/Hungary-Flag-24.png");
		JLabel hufL=new JLabel(hufI);
		hufL.setToolTipText("Hungarian fiorint");
		
		ImageIcon ltlI=new ImageIcon("resources/png/Lithuania-Flag-24.png");
		JLabel ltlL=new JLabel(ltlI);
		ltlL.setToolTipText("Lithuanian litas");
		
		ImageIcon lvlI=new ImageIcon("resources/png/Latvia-Flag-24.png");
		JLabel lvlL=new JLabel(lvlI);
		lvlL.setToolTipText("Latvian lats");
		
		ImageIcon plnI=new ImageIcon("resources/png/Poland-Flag-24.png");
		JLabel plnL=new JLabel(plnI);
		plnL.setToolTipText("Polish zloty");
		
		ImageIcon ronI=new ImageIcon("resources/png/Romania-Flag-24.png");
		JLabel ronL=new JLabel(ronI);
		ronL.setToolTipText("Romanian leu");

		ImageIcon sekI=new ImageIcon("resources/png/Sweden-Flag-24.png");
		JLabel sekL=new JLabel(sekI);
		sekL.setToolTipText("Swedish krona");
		
		ImageIcon chfI=new ImageIcon("resources/png/Switzerland-Flag-24.png");
		JLabel chfL=new JLabel(chfI);
		chfL.setToolTipText("Swiss franc");
		
		ImageIcon nokI=new ImageIcon("resources/png/Norway-Flag-24.png");
		JLabel nokL=new JLabel(nokI);
		nokL.setToolTipText("Norwegian krone");
		
		ImageIcon hrkI=new ImageIcon("resources/png/Croatian-Flag-24.png");
		JLabel hrkL=new JLabel(hrkI);
		hrkL.setToolTipText("Croatian Kuna");
		
		ImageIcon rubI=new ImageIcon("resources/png/Russia-Flag-24.png");
		JLabel rubL=new JLabel(rubI);
		rubL.setToolTipText("Russian ruble");
		
		ImageIcon tryI=new ImageIcon("resources/png/Turkey-Flag-24.png");
		JLabel tryL=new JLabel(tryI);
		tryL.setToolTipText("Turkish lira");
		
		ImageIcon audI=new ImageIcon("resources/png/Australia-Flag-24.png");
		JLabel audL=new JLabel(audI);
		audL.setToolTipText("Australian dollar");
		
		ImageIcon brlI=new ImageIcon("resources/png/Brazil-Flag-24.png");
		JLabel brlL=new JLabel(brlI);
		brlL.setToolTipText("Brazil real");
		
		ImageIcon cadI=new ImageIcon("resources/png/Canada-Flag-24.png");
		JLabel cadL=new JLabel(cadI);
		cadL.setToolTipText("Canadian dollar");
		
		ImageIcon cnyI=new ImageIcon("resources/png/China-Flag-24.png");
		JLabel cnyL=new JLabel(cnyI);
		cnyL.setToolTipText("Chinese yuan");
		
		ImageIcon hkdI=new ImageIcon("resources/png/Hong-Kong-Flag-24.png");
		JLabel hkdL=new JLabel(hkdI);
		hkdL.setToolTipText("Hong Kong dollar");
		
		ImageIcon idrI=new ImageIcon("resources/png/Indonesia-Flag-24.png");
		JLabel idrL=new JLabel(idrI);
		idrL.setToolTipText("Indonesian rupiah");
		
		ImageIcon ilsI=new ImageIcon("resources/png/Israel-Flag-24.png");
		JLabel ilsL=new JLabel(ilsI);
		ilsL.setToolTipText("Israeli new sheqel");
		
		ImageIcon inrI=new ImageIcon("resources/png/India-Flag-24.png");
		JLabel inrL=new JLabel(inrI);
		inrL.setToolTipText("Indian rupee");
		
		ImageIcon krwI=new ImageIcon("resources/png/Korea-Flag-24.png");
		JLabel krwL=new JLabel(krwI);
		krwL.setToolTipText("South Korean won");
		
		ImageIcon mxnI=new ImageIcon("resources/png/Mexico-Flag-24.png");
		JLabel mxnL=new JLabel(mxnI);
		mxnL.setToolTipText("Mexican peso");
		
		ImageIcon myrI=new ImageIcon("resources/png/Malaysia24.png");
		JLabel myrL=new JLabel(myrI);
		myrL.setToolTipText("Malaysian ringgit");
		
		ImageIcon nzdI=new ImageIcon("resources/png/New-Zealand24.png");
		JLabel nzdL=new JLabel(nzdI);
		nzdL.setToolTipText("New Zealand dollar");
		
		ImageIcon phpI=new ImageIcon("resources/png/Philippines-Flag-24.png");
		JLabel phpL=new JLabel(phpI);
		phpL.setToolTipText("Philippine peso");
		
		ImageIcon sgdI=new ImageIcon("resources/png/Singapore-Flag-24.png");
		JLabel sgdL=new JLabel(sgdI);
		sgdL.setToolTipText("Singapore dollar");
		
		ImageIcon thbI=new ImageIcon("resources/png/Thailand-Flag-24.png");
		JLabel thbL=new JLabel(thbI);
		thbL.setToolTipText("Thai baht");
		
		ImageIcon zarI=new ImageIcon("resources/png/South-Africa-Flag-24.png");
		JLabel zarL=new JLabel(zarI);
		zarL.setToolTipText("South African rand");
		
		JLabel dateL=new JLabel("On date:");
		JLabel logged=new JLabel("Logged in as: " + uname);
		logged.setForeground(Color.green);
		logged.setFont(new Font("Serif", Font.ITALIC, 16));
		
		

		final JFrame conFrame=new JFrame("Currency Converter");
		JPanel p=new JPanel();
		p.setLayout(new MigLayout
				("","40[][]30[][]30[][]30[][]30[][]30[][]","[][]40"));
		conFrame.pack();
		conFrame.setContentPane(p);
		conFrame.setVisible(true);
		conFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		conFrame.setResizable(false);
		conFrame.setSize(1024,600);
		JRootPane rootPane = conFrame.getRootPane();
	    rootPane.setDefaultButton(conv);
	    exit.setMnemonic(KeyEvent.VK_ESCAPE);
		conFrame.add(eurL,"align right, span 6, wrap");
		conFrame.add(eurF,"align right, span 6");
		conFrame.add(conv,"wrap");
		conFrame.add(usaF);
		conFrame.add(usaL);
		conFrame.add(jpnF);
		conFrame.add(jpnL);
		conFrame.add(bulF);
		conFrame.add(bulL);
		conFrame.add(czkF);
		conFrame.add(czkL);
		conFrame.add(dkkF);
		conFrame.add(dkkL);
		conFrame.add(gbpF);
		conFrame.add(gbpL,"wrap");
		conFrame.add(hufF);
		conFrame.add(hufL);
		conFrame.add(ltlF);
		conFrame.add(ltlL);
		conFrame.add(lvlF);
		conFrame.add(lvlL);
		conFrame.add(plnF);
		conFrame.add(plnL);
		conFrame.add(ronF);
		conFrame.add(ronL);
		conFrame.add(sekF);
		conFrame.add(sekL,"wrap");
		conFrame.add(chfF);
		conFrame.add(chfL);
		conFrame.add(nokF);
		conFrame.add(nokL);
		conFrame.add(hrkF);
		conFrame.add(hrkL);
		conFrame.add(rubF);
		conFrame.add(rubL);
		conFrame.add(tryF);
		conFrame.add(tryL);
		conFrame.add(audF);
		conFrame.add(audL,"wrap");
		conFrame.add(brlF);
		conFrame.add(brlL);
		conFrame.add(cadF);
		conFrame.add(cadL);
		conFrame.add(cnyF);
		conFrame.add(cnyL);
		conFrame.add(hkdF);
		conFrame.add(hkdL);
		conFrame.add(idrF);
		conFrame.add(idrL);
		conFrame.add(ilsF);
		conFrame.add(ilsL,"wrap");
		conFrame.add(inrF);
		conFrame.add(inrL);
		conFrame.add(krwF);
		conFrame.add(krwL);
		conFrame.add(mxnF);
		conFrame.add(mxnL);
		conFrame.add(myrF);
		conFrame.add(myrL);
		conFrame.add(nzdF);
		conFrame.add(nzdL);
		conFrame.add(phpF);
		conFrame.add(phpL,"wrap");
		conFrame.add(sgdF);
		conFrame.add(sgdL);
		conFrame.add(thbF);
		conFrame.add(thbL);
		conFrame.add(zarF);
		conFrame.add(zarL,"wrap");
		conFrame.add(dateL,"span 2,align right");
		conFrame.add(dateF,"span 5");
		conFrame.add(logged,"span 2");
		conFrame.add(exit,"span 2,align left");
		
		 final Timer timer = new Timer(300000, new ActionListener() {
	            public void actionPerformed(ActionEvent e) {
	            	conFrame.setVisible(false);
					try {
						new Entry();
					} catch (IOException ae) {
						// TODO Auto-generated catch block
						ae.printStackTrace();
					}
	            }
	            
	        });
		 timer.start();
		//action listener exit
		ActionListener actionListenerExit=new ActionListener()	{
			public void actionPerformed(ActionEvent actionEvent)	{
				conFrame.setVisible(false);
				try {
					new Entry();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			
			
		};
		
		exit.setMnemonic(KeyEvent.VK_ESCAPE);
		exit.registerKeyboardAction
		(actionListenerExit, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 
				JComponent.WHEN_IN_FOCUSED_WINDOW);
		
		ActionListener actionListenerConvertTime=new ActionListener()	{
			public void actionPerformed(ActionEvent actionEvent)	{
				timer.restart();
				
			}
			
			
		};
		
		ActionListener actionListenerConvert=new ActionListener()	{
			public void actionPerformed(ActionEvent actionEvent)		{
				try {
					Convert.convert(eurF.getText());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					
					}
			

			
			
		};	
		exit.addActionListener(actionListenerExit);
		conv.addActionListener(actionListenerConvertTime);
		conv.addActionListener(actionListenerConvert);
	}

	

}